//
//  PicCollectionView.swift
//  JGWB
//
//  Created by 付建国 on 2016/10/13.
//  Copyright © 2016年 sher. All rights reserved.
//

import UIKit
import SDWebImage

class JGPicCollectionView: UICollectionView {

    // MARK:- 定义属性
    var picURLs : [URL] = [URL](){
        didSet{
            self.reloadData()
        }
    }
    
    // MARK:- 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
        
        dataSource = self
    }

}
// MARK:- collectionView的数据源方法
extension JGPicCollectionView : UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picURLs.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PicCell", for: indexPath) as! JGPicCollectionViewCell
        
        cell.picURL = picURLs[indexPath.row]
        
        return cell
        
    }
    
}

class JGPicCollectionViewCell : UICollectionViewCell {
    
    // MARK:- 定义模型属性
    var picURL : URL?{
        didSet{
            guard let picURL = picURL else {
                return
            }
            
            picImageView.sd_setImage(with: picURL, placeholderImage: UIImage.init(named: "empty_picture"))
            
        }
    }
    
    // MARK:- 控件的属性
    @IBOutlet weak var picImageView: UIImageView!
}















